﻿#include "product.h"
#include <QJsonObject>
#include "tools.h"

Product::Product(QObject *parent) : QObject(parent)
{
    lowerLimit = 0;
    upperLimit = 0;
    count = 0;
    sanbao = 0;
    inPrice = 0.0;
    guidePrice1 = 0.0;
    guidePrice2 = 0.0;
}

Product::Product(QJsonObject obj){
    loadFromJsonObject(obj);
}

void Product::loadFromJsonObject(QJsonObject obj)
{
    id = obj.value("id").toInt();
    productId = obj.value("product_id").toString();
    companyId = obj.value("company_id").toString();
    type = obj.value("type").toString();
    typePinyin = obj.value("type_pinyin").toString();
    name = obj.value("name").toString();
    namePinyin = obj.value("name_pinyin").toString();
    unit = obj.value("unit").toString();
    productNum = obj.value("product_num").toString();
    upperLimit = obj.value("upper_limit").toInt();
    lowerLimit = obj.value("lower_limit").toInt();

    brand = obj.value("brand").toString();
    originPlace = obj.value("origin_place").toString();
    specifications = obj.value("specifications").toString();
    barCode = obj.value("bar_code").toString();
    inPrice = obj.value("in_price").toDouble();
    remark = obj.value("remark").toString();

    guidePrice1 = obj.value("guide_price1").toDouble();
    guidePrice2 = obj.value("guide_price2").toDouble();
    primaryDirectory = obj.value("primary_directory").toString();
    secondaryDirectory = obj.value("secondary_directory").toString();

    count = obj.value("count").toInt();
    sanbao = obj.value("sanbao").toInt();

//    Tools::cout("id:"+QString("%1").arg(id));
//    Tools::cout("productId:"+companyId);
//    Tools::cout("companyId:"+companyId);
//    Tools::cout("type:"+type);
//    Tools::cout("typePinyin:"+typePinyin);
//    Tools::cout("name:"+name);
//    Tools::cout("namePinyin:"+namePinyin);
//    Tools::cout("unit:"+unit);
//    Tools::cout("productNum:"+productNum);
//    Tools::cout("upperLimit:"+QString("%1").arg(upperLimit));
//    Tools::cout("lowerLimit:"+QString("%1").arg(lowerLimit));

//    Tools::cout("brand:"+brand);
//    Tools::cout("originPlace:"+originPlace);
//    Tools::cout("specifications:"+specifications);
//    Tools::cout("barCode:"+barCode);
//    Tools::cout("inPrice:"+QString("%1").arg(inPrice));
//    Tools::cout("remark:"+remark);

//    Tools::cout("guidePrice1:"+QString("%1").arg(guidePrice1));
//    Tools::cout("guidePrice2:"+QString("%1").arg(guidePrice2));
//    Tools::cout("primaryDirectory:"+primaryDirectory);
//    Tools::cout("secondaryDirectory:"+secondaryDirectory);

//    Tools::cout("count:"+QString("%1").arg(count));
//    Tools::cout("sanbao:"+QString("%1").arg(sanbao));
}

void Product::loadFromSqlQuery(QSqlQuery query)
{
    id = query.value(0).toInt();
    productId = query.value(1).toString();
    companyId = query.value(2).toString();
    type = query.value(3).toString();
    typePinyin = query.value(4).toString();
    name = query.value(5).toString();
    namePinyin = query.value(6).toString();
    unit = query.value(7).toString();
    upperLimit = query.value(8).toInt();
    lowerLimit = query.value(9).toInt();
    productNum = query.value(10).toString();
    count = query.value(11).toInt();
    sanbao = query.value(12).toInt();
    inPrice = query.value(13).toDouble();
    specifications = query.value(14).toString();
    brand = query.value(15).toString();
    originPlace = query.value(16).toString();
    remark = query.value(17).toString();
    barCode = query.value(18).toString();
    guidePrice1 = query.value(19).toDouble();
    guidePrice2 = query.value(20).toDouble();
    primaryDirectory = query.value(21).toString();
    secondaryDirectory = query.value(22).toString();
}

QString Product::toJson()
{
    QString jsonStr = "{";

    jsonStr.append("\"id\":"+QString("%1").arg(id)+",");
    jsonStr.append("\"product_id\":"+Tools::toJsonStr(productId)+",");
    jsonStr.append("\"company_id\":"+Tools::toJsonStr(companyId)+",");
    jsonStr.append("\"type\":"+Tools::toJsonStr(type)+",");
    jsonStr.append("\"name\":"+Tools::toJsonStr(name)+",");
    jsonStr.append("\"unit\":"+Tools::toJsonStr(unit)+",");
    jsonStr.append("\"product_num\":"+Tools::toJsonStr(productNum)+",");
    jsonStr.append("\"upper_limit\":"+QString("%1").arg(upperLimit)+",");
    jsonStr.append("\"lower_limit\":"+QString("%1").arg(lowerLimit)+",");
    jsonStr.append("\"count\":"+QString("%1").arg(count)+",");
    jsonStr.append("\"count_num\":"+QString("%1").arg(countNum)+",");
    jsonStr.append("\"sanbao\":"+QString("%1").arg(sanbao)+",");
    jsonStr.append("\"brand\":"+Tools::toJsonStr(brand)+",");
    jsonStr.append("\"origin_place\":"+Tools::toJsonStr(originPlace)+",");
    jsonStr.append("\"specifications\":"+Tools::toJsonStr(specifications)+",");
    jsonStr.append("\"barCode\":"+Tools::toJsonStr(barCode)+",");
    jsonStr.append("\"in_price\":"+QString("%1").arg(inPrice)+",");
    jsonStr.append("\"remark\":"+Tools::toJsonStr(remark)+",");
    jsonStr.append("\"guide_price1\":"+QString("%1").arg(guidePrice1)+",");
    jsonStr.append("\"guide_price2\":"+QString("%1").arg(guidePrice2)+",");
    jsonStr.append("\"primary_directory\":"+Tools::toJsonStr(primaryDirectory)+",");
    jsonStr.append("\"secondary_directory\":"+Tools::toJsonStr(secondaryDirectory));

    jsonStr.append("}");

    return jsonStr;
}

bool Product::checkExists(QList<Product*> products){
    if(products.size() == 0){
        return false;
    }

    if(this->productId.isEmpty()){
        return false;
    }

    for(Product *product:products){
        if(product->productId == this->productId){
            return true;
        }
    }

    return false;
}
